package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.VendorCategoryMapper;
import org.linlinjava.litemall.db.domain.VendorCategory;
import org.linlinjava.litemall.db.domain.VendorCategory;
import org.linlinjava.litemall.db.domain.VendorCategoryExample;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class VendorCategoryService {
    @Resource
    private VendorCategoryMapper vendorCategoryMapper;

    /**
     * 根据当前商户的类目列表
     * @return
     */
    public List<VendorCategory> queryCategories(String name, Integer page, Integer limit, String sort, String order) {
        VendorCategoryExample example = new VendorCategoryExample();
        VendorCategoryExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, limit);
        return vendorCategoryMapper.selectByExample(example);
    }


    /**
     * 根据当前商户的类目列表
     * @return
     */
    public List<VendorCategory> queryAllCategories(String sort, String order) {
        VendorCategoryExample example = new VendorCategoryExample();
        VendorCategoryExample.Criteria criteria = example.createCriteria();

        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        return vendorCategoryMapper.selectByExample(example);
    }

    /**
     * 添加标签
     * @param tag
     * @return
     */
    public boolean addCategory(VendorCategory tag) {
        return vendorCategoryMapper.insertSelective(tag) > 0;
    }

    /**
     * 删除类目
     * @param categoryId
     * @return
     */
    public boolean deleteById(long categoryId) {
        VendorCategoryExample example = new VendorCategoryExample();
        example.or().andIdEqualTo(categoryId);
        return vendorCategoryMapper.deleteByPrimaryKey(categoryId) > 0;
    }

    public VendorCategory findById(Long id) {
        return vendorCategoryMapper.selectByPrimaryKey(id);
    }

    /**
     * 更新标签
     * @param category
     * @return
     */
    public int updateById(VendorCategory category) {
        category.setUpdateTime(LocalDateTime.now());
        return vendorCategoryMapper.updateByPrimaryKeySelective(category);
    }
}
